// 处理文件相关
import { ref,reactive,inject,getCurrentInstance } from 'vue'
import {useStore} from 'vuex'
import { randomString,getCurrentTime } from '@/utils/util'
import appInit from '@/hook/appInit'

export default function(context) {
    const store = useStore()
    const { proxy } = getCurrentInstance();
    const viewMode = proxy.$config.viewMode
    const openWindow = inject('openWindow')

    let initData = appInit()
    let SelectId = ref(1001)
    // let fileNavArray = reactive(['application/'])

    // context.emit('openNav',fileNavArray)
    
    // const fileMenu = inject('fileMenu')
    // const openWindow = inject('openWindow')
    // const AppMenu = inject('AppMenu')

    // 处理右键菜单
    // function openFileMenu(e,path){
    //     fileMenu(e,path)
    // }

    // 处理应用打开窗口
    function handleOpen(value){
        openWindow(value)
    }

    // function openAppMenu(e,value){
    //     AppMenu(e,value)
    // }

    // 处理文件分区选择
    function handleSelect(value){
        SelectId.value = value.id
        store.dispatch('handleFileNav',value)
        // fileNavArray.length = 0
        // fileNavArray.push(value.key+'/')
        // context.emit('openNav',fileNavArray)
    }

    // 处理文件分区视图模式
    function handleViewMode(value1,value2){
        value1.viewMode = value2
        let timer = null
        if(timer){
            clearTimeout(timer)
        }
        timer = setTimeout(() => {
            let fileCache = proxy.$utils.cache.file.get()||null
            if(fileCache){
                let newArr = JSON.parse(fileCache)
                newArr.forEach(item=> {
                    if(item.id===SelectId.value){
                        item.viewMode = value2
                    }
                });
                proxy.$utils.cache.file.set(JSON.stringify(newArr))
            }
        }, 1000);
    }

    // 新建文件夹
    function createFolder(){
        let fileNavStr = store.state.fileNavArray.join()
        let fileNavArr1 = fileNavStr.split('/')
        let fileNavArr2 = fileNavArr1.slice(0,fileNavArr1.length-1)
        let fileCache = proxy.$utils.cache.file.get()||null
        if(fileCache){
            let filesData = JSON.parse(fileCache)
            digui(filesData,filesData,fileNavArr2,0)
        }
    }

    function digui(origin,target,path,count){
        let newFolder = {}
        target.forEach(item => {
            if(item.key===path[count]){
                if(count<=path.length-1||count>=path.length-1){
                    // newFolder.key=path[count]
                    newFolder.type='folder'
                    newFolder.id = randomString(10)
                    newFolder.createdAt = getCurrentTime()
                    newFolder.icon = require('@/assets/icon/file.png')
                    let folderArr = item.child.filter(item1 => {
                        return item1.type==='folder'
                    });
                    newFolder.name=`新建文件夹${folderArr.length+1}`
                    newFolder.key=path[count]+`新建文件夹${folderArr.length+1}`
                    item.child.push(newFolder)
                    console.log('创建成功',origin);
                    proxy.$utils.cache.file.set(JSON.stringify(origin))
                    setTimeout(() => {
                        initData.init()
                    }, 300);
                }else{
                    count++
                    digui(origin,item.child,path,count)
                }
            }
        });
    }

    

    return {
        SelectId,
        viewMode,
        handleSelect,
        // openAppMenu,
        // openFileMenu,
        handleOpen,
        handleViewMode,
        createFolder,
        ...initData
        // ...appInit()
    }
}